package edu.gatech.cs2340.group7.wheresmystuffapp.views;

import java.io.InputStream;

import edu.gatech.cs2340.group7.wheresmystuffapp.R;
import edu.gatech.cs2340.group7.wheresmystuffapp.presenters.ShowItemPresenter;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ShowItemView extends Activity implements OnClickListener {
	@SuppressWarnings("unused")
	private ShowItemPresenter myPresenter;
	private ImageView ivPicture;
	private TextView tvItemName;
	private TextView tvCategory;
	private TextView tvDescription;
	private TextView tvItemDate;
	private TextView tvLocation;
	private Button bCancel;
	private String PACKAGE_NAME;

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show_item_info);
		linkSetup();
		PACKAGE_NAME =getApplication().getPackageName();
		myPresenter = new ShowItemPresenter(this);
		
	}

	@Override
	public void onClick(View arg0) {
		Intent intent = new Intent(this, UserMainMenuView.class);
		intent.setPackage(PACKAGE_NAME);
		startActivity(intent);		
	}
	
	private void linkSetup() {
		bCancel = (Button) findViewById(R.id.Item_Info_Back_Button);
		bCancel.setOnClickListener(this);
		
		tvCategory = (TextView) findViewById(R.id.Item_Info_Item_Category);
		ivPicture = (ImageView) findViewById(R.id.Item_Info_Picture_View);
		tvItemName = (TextView) findViewById(R.id.Item_Info_Item_Name);
		tvDescription = (TextView) findViewById(R.id.Item_Info_Item_Description);
		tvItemDate = (TextView) findViewById(R.id.Item_Info_Item_Date);
		tvLocation = (TextView) findViewById(R.id.Item_Info_Item_Location);
		
	}
	
	public void setImage(byte[] picBits) {
		try {
			Bitmap bitmap = BitmapFactory.decodeByteArray(picBits, 0, picBits.length);

			if (bitmap != null) {

				ivPicture.setImageBitmap(bitmap);
			}
			else{
				Bitmap bmp;
				InputStream is = getResources().openRawResource(R.drawable.no_photo);
				bmp = BitmapFactory.decodeStream(is);
				ivPicture.setImageBitmap(bmp);
			}
		}
		catch (Exception e){
			System.out.println("Picture Not found");
			Bitmap bmp;
			InputStream is = getResources().openRawResource(R.drawable.no_photo);
			bmp = BitmapFactory.decodeStream(is);
			ivPicture.setImageBitmap(bmp);
		}
	}
	
	public void setTextViews(String name, String desc, String cat, String itemDate, String location) {
		tvItemName.setText(name);
		tvDescription.setText(desc);
		tvCategory.setText(cat);
		tvItemDate.setText(itemDate);
		tvLocation.setText(location);
	}

}
